<?php
include 'AesServer.php';
include 'httpclient.php';

function execute($url, $charset, $data, $privateKeyName, $password, $publicKeyName, $domain, $charset_change_fields, $encrypted_fields, $special_fields, $json_array_fields, $defaultCharset)
{
    $aes = new CryptAES();
    $aesKey = $aes->getAesKey();
    $aes->set_key($aesKey);
    $aes->require_pkcs5();
    $data = processJSONArrayFields($data, $charset, $charset_change_fields, $encrypted_fields, $special_fields, $json_array_fields, $defaultCharset, $aes);
    $data = charsetChange($charset_change_fields, $data, $charset, $defaultCharset);
    $data = encryptByAesKey($encrypted_fields, $data, $aes);
    $aesKey = encrpyt(base64_encode($aesKey), $publicKeyName);
    $data['aes_key'] = $aesKey;
    $data = specialChange($special_fields, $data);
    $signStr = sign(getStr($data), $privateKeyName, $password);
    $data['sign'] = $signStr;
    $data['sign_type'] = 'CERT';
    $optional_headers =  Array("Referer: " . $domain);
    return do_post_request($url, $data, $optional_headers);
}

function processJSONObjectFields($data, $charset, $charset_change_fields, $encrypted_fields, $special_fields, $json_fields, $defaultCharset, $aes){
    foreach ($json_fields as $key) {
        if (isset($data[$key])) {
            $json_obejct = json_decode($data[$key], TRUE);
            $json_obejct= charsetChange($charset_change_fields, $json_obejct, $charset, $defaultCharset);
            $json_obejct= encryptByAesKey($encrypted_fields, $json_obejct, $aes);
            $json_obejct= specialChange($special_fields, $json_obejct);
            $data[$key] = json_encode($json_obejct);
        }
    }
    return $data;
}

function processJSONArrayFields($data, $charset, $charset_change_fields, $encrypted_fields, $special_fields, $json_array_fields, $defaultCharset, $aes){
    foreach ($json_array_fields as $key) {
        if (isset($data[$key])) {
            $json_array = json_decode($data[$key], TRUE);
            for($i=0;$i<count($json_array);$i++) {
                $json_obejct = $json_array[$i];
                $json_obejct = charsetChange($charset_change_fields, $json_obejct, $charset, $defaultCharset);
                $json_obejct = encryptByAesKey($encrypted_fields, $json_obejct, $aes);
                $json_obejct = specialChange($special_fields, $json_obejct);
                $json_array[$i] = $json_obejct;
            }
            $data[$key] = json_encode($json_array);
        }
    }
    return $data;
}

function charsetChange($charset_change_fields, $data, $merCharset, $defaultCharset)
{
    if($defaultCharset == $merCharset){
        return $data;
    }
    foreach ($charset_change_fields as $key) {
        if (isset($data[$key])) {
            $value = $data[$key];
            if ($value && "" != $value) {
                $data[$key] = iconv($merCharset, $defaultCharset, $value);
            }
        }
    }
    return $data;
}

function encryptByAesKey($encrypted_fields, $data, $aes)
{
    foreach ($encrypted_fields as $key) {
        if (isset($data[$key])) {
            $value = $data[$key];
            if ($value && "" != $value) {
                if(phpversion() >= 7.1){
                    $data[$key] = $aes->aes_encode($value);
                }else{
                    $data[$key] = $aes->encrypt($value);
                }
            }
        }
    }
    return $data;
}

function specialChange($special_fields, $data)
{
    foreach ($special_fields as $key) {
        if (isset($data[$key])) {
            $value = $data[$key];
            if ($value && "" != $value) {
                $data[$key] = base64_encode($value);
            }
        }
    }
    return $data;
}

?>